<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $link_max;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_warp_title']);

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Run checks to ensure player enough turns and warp edit devices
if ($playerinfo['turns'] < 1)
{
	echo $txt['bnt_warp_turn'], '<br><br>';
	TextMainMenu();
	return;
}

if ($playerinfo['dev_warpedit'] < 1)
{
	echo $txt['bnt_warp_none'], '<br><br>';
	TextMainMenu();
	return;
}

// Run check to ensure player can edit warp links in this sector.
$result = $smcFunc['db_query']('', '
	SELECT allow_warpedit, {raw:db_universe}.zone_id
	FROM {raw:db_zones}, {raw:db_universe}
	WHERE sector_id = {int:sector}
	AND {raw:db_universe}.zone_id = {raw:db_zones}.zone_id',
	array(
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones'],
		'sector' => $playerinfo['sector'],
	)
);
$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_warpedit'] == 'N')
{
	echo $txt['bnt_warp_forbid'], '<br><br>';
	TextMainMenu();
	return;
}

// Player is able to edit warp links, so read the post variables
if (isset($_POST['target_sector']))
{
	$target_sector = round($_POST['target_sector']);
	$oneway = (isset($_POST['oneway'])) ? $_POST['oneway'] : '';
}
else
{
	echo $txt['bnt_warp_nosector'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:target_sector}',
	array(
		'db_universe' => $dbtables['universe'],
		'target_sector' => $target_sector
	)
);

if ($smcFunc['db_num_rows']($result) == 0)
{
	echo $txt['bnt_warp_nosector'], '<br><br>';
	TextMainMenu();
	return;
}
$smcFunc['db_free_result']($result);

$result = $smcFunc['db_query']('', '
	SELECT allow_warpedit, {raw:db_universe}.zone_id
	FROM {raw:db_zones}, {raw:db_universe}
	WHERE sector_id = {int:target_sector}
	AND {raw:db_universe}.zone_id = {raw:db_zones}.zone_id',
	array(
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones'],
		'target_sector' => $target_sector
	)
);
$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_warpedit'] == 'N' && empty($oneway))
{
	$txt['bnt_warp_twoerror'] = str_replace("[target_sector]", $target_sector, $txt['bnt_warp_twoerror']);
	echo $txt['bnt_warp_twoerror'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_links}
	WHERE link_start = {int:sector}',
	array(
		'db_links' => $dbtables['links'],
		'sector' => $playerinfo['sector']
	)
);
$numlink_start = $smcFunc['db_num_rows']($result);

if ($numlink_start >= $link_max )
{
	echo $txt['bnt_warp_sectex'], '<br><br>';
	TextMainMenu();
	return;
}

$flag = 0;
$flag2 = 0;

if ($smcFunc['db_num_rows']($result) > 0)
{
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		if ($target_sector == $row['link_dest'])
			$flag = 1;
	}
}
$smcFunc['db_free_result']($result);
	
if ($flag == 1)
{
	$txt['bnt_warp_linked'] = str_replace("[target_Sector]", $target_sector, $txt['bnt_warp_linked']);
	echo $txt['bnt_warp_linked'], '<br><br>';
}
elseif ($playerinfo['sector'] == $target_sector)
	echo $txt['bnt_warp_cantsame'];
else
{
	$smcFunc['db_insert']('insert',
		$dbtables['links'],
		array(
			'link_start' => 'int',
			'link_dest' => 'int',
		),
		array(
			$playerinfo['sector'],
			$target_sector
		),
		''
	);
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET dev_warpedit = dev_warpedit - 1, 
			turns = turns - 1, 
			turns_used = turns_used + 1
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	if (!empty($oneway))
		echo $txt['bnt_warp_coneway'], ' ', $target_sector, '<br><br>';
	else
	{
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_links}
			WHERE link_start = {int:target_sector}',
			array(
				'db_links' => $dbtables['links'],
				'target_sector' => $target_sector
			)
		);
		
		if ($smcFunc['db_num_rows']($result) == 0)
		{
			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				if ($playerinfo['sector'] == $row['link_dest'])
					$flag2 = 1;
			}
		}
		
		if ($flag2 != 1)
			$smcFunc['db_insert']('insert',
				$dbtables['links'],
				array(
					'link_start' => 'int',
					'link_dest' => 'int',
				),
				array(
					$target_sector,
					$playerinfo['sector']
				),
				''
			);
			
		echo $txt['bnt_warp_ctwoway'], ' ', $target_sector, '<br><br>';
	}
}

TextMainMenu();
?>
